/*
* BlockFrequency.cpp
* Last edited: 21.07.2014
* Author: David Noack
*/

#include "BlockFrequency.h"

//Inittialize the base class with the input data
BlockFrequency::BlockFrequency(bool* bitArray, int bitArraySize) : NistTest(bitArray, bitArraySize){};


double BlockFrequency::execute(int blockSize, int size)
{
	n = size;
	int blockNumber = size / blockSize;
	int blocksum = 0;
	double pi = 0.0;
	double pi_sum = 0.0;
	double chi_squared = 0.0;

	//tasks per block
	for (int i = 0; i < blockNumber; i++)
	{
		blocksum = 0;
		//sum all ones of a block
		for (int j = 0; j < blockSize; j++)
		{
			blocksum += (int)bitArray[j + i*blockSize];
		}
		//divide the sums of ones through the blocksize
		pi = (double)blocksum / (double)blockSize;
		pi = pi - 0.5;
		pi_sum += (pi*pi);
	}

	chi_squared = 4 * blockSize * pi_sum;

	p_value = boost::math::gamma_q(blockNumber / 2.0, chi_squared / 2.0);

	return p_value;
}
